package com.testapp.servlet;

import java.io.IOException;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.testapp.Comment;
import com.testapp.GAIPersistence;

@SuppressWarnings("serial")
public class AddCommentServlet extends HttpServlet {
	public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String fromPerson = req.getParameter("fromPerson");
		String comment = req.getParameter("commentMade");
		
		if(fromPerson.length() == 0 && comment.length() == 0) {
			resp.sendRedirect("/addComment.jsp?error=true");
			return;
		} else {
			if(fromPerson.length() == 0) {
				fromPerson = "Anonymous";
			}
		}

		PersistenceManager pm = GAIPersistence.getFactory().getPersistenceManager();
		Comment cmnt = new Comment(fromPerson, comment);
		try {
			pm.makePersistent(cmnt);
			resp.sendRedirect("/comments.jsp");
		} finally {
			pm.close();
		}
	}
}
